﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using wpf_print.utils;

namespace wpf_print.utils
{
    internal class PrintData
    {
        public class ObjectInfo
        {
            public double Width { get; set; }
            public double Height { get; set; }
        }
        public class PrintListInfo
        {
            public string? PrintName { get; set; }
            public string? PrintValue { get; set; }
        }
        public class PrintPageInfo
        {
            public string? PrintName { get; set; }
            public ObjectInfo? PrintValue { get; set; }
        }
        //修改默认打印机
        [DllImport("winspool.drv")]
        public static extern void SetDefaultPrinter(String name);

        public static List<PrintPageInfo> GetPageData() {
            List<PrintPageInfo> list = new();
                PrintPageInfo s = new();
                s.PrintValue = new ObjectInfo() {
                    Width = 852,
                    Height = 1090
                };
                s.PrintName = "信纸";
                list.Add(s);
            return list;
        }
        private static readonly PrintDocument fPrintDocument = new();
        /// <summary>
        /// 获取本机默认打印机名称
        /// </summary>
        /// <returns></returns>
        public static string DefaultPrinter()
        {
            return fPrintDocument.PrinterSettings.PrinterName;
        }

        /// <summary>
        /// 获取打印机列表
        /// </summary>
        /// <returns></returns>
        public static List<PrintListInfo> GetLocalPrinters()
        {
            List<string> fPrinters = new();
            List<PrintListInfo> reList = new();
            fPrinters.Add(DefaultPrinter()); //默认打印机始终出现在列表的第一项
            reList.Add(new PrintListInfo()
            {
                PrintName = DefaultPrinter(),
                PrintValue = DefaultPrinter()
            });
            foreach (string fPrinterName in PrinterSettings.InstalledPrinters)
            {
                if (!fPrinters.Contains(fPrinterName))
                {
                    reList.Add(new PrintListInfo()
                    {
                        PrintName = fPrinterName,
                        PrintValue = fPrinterName
                    });
                }
            }
            reList.Add(new PrintListInfo()
            {
                PrintName = "Microsoft Print to PDF",
                PrintValue = "Microsoft Print to PDF"
            });
            return reList;
        }
    }
}
